package com.itheima.service.impl;


import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService{


    @Autowired
    private OrderSettingDao orderSettingDao;

    @Override
    public void addByXlsx(List<String[]> list) {
        //将controller传递过来的集合遍历添加到数据库
        for (String[] strings : list) {
            //在进行 导入数据之前先查询数据库中是否存在当前数据
            long l = orderSettingDao.searchOrderSettingByDate(strings[0]);
            //将数据封装到OrderSetting实体类中
            //最好用simpleDateFormat来进行日期类的转换
            OrderSetting orderSetting = new OrderSetting(new Date(strings[0]),Integer.parseInt(strings[1]));
            if (l > 0){
                //分类处理 ，如果数据库中存在与导入的xlsx中相同的数据则进行update操作
                orderSettingDao.editOrderSetting(orderSetting);
            }else{
                //若没有则进行insert操作
                orderSettingDao.addOrderSetting(orderSetting);
            }
        }
    }

    @Override
    public List<Map<String, Object>> searchOrderSettingByMonth(String date) {

        String firstDate = "";
        String lastDate = "";
        if (date.split("-")[1].length()==1){
            firstDate=date.split("-")[0]+"-0"+date.split("-")[1]+"-01";
            lastDate=date.split("-")[0]+"-0"+date.split("-")[1]+"-31";
        }else{
            firstDate=date+"-01";
            lastDate=date+"-31";
        }

        System.out.println(firstDate);
        System.out.println(lastDate);

        List<OrderSetting> list = orderSettingDao.searchOrderSettingByMonth(firstDate, lastDate);

        List<Map<String,Object>> map = new ArrayList<>();
        if (list != null && list.size() > 0){
            for (OrderSetting orderSetting : list) {
                Map<String,Object> p = new HashMap<>();
                p.put("date", orderSetting.getOrderDate().getDate());
                p.put("number", orderSetting.getNumber());
                p.put("reservations", orderSetting.getReservations());
                map.add(p);
            }
        }

        return map;
    }

    @Override
    public void settingOrderNumber(OrderSetting orderSetting) {
        //在进行 导入数据之前先查询数据库中是否存在当前数据
        //System.out.println(orderSetting.getOrderDate().toString());
        //Wed Sep 22 00:00:00 CST 2021
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String format = simpleDateFormat.format(orderSetting.getOrderDate());
        System.out.println(format);
        long l = orderSettingDao.searchOrderSettingByDate(format);
        //将数据封装到OrderSetting实体类中
        //最好用simpleDateFormat来进行日期类的转换);
        if (l > 0){
            //分类处理 ，如果数据库中存在与导入的xlsx中相同的数据则进行update操作
            orderSettingDao.editOrderSetting(orderSetting);
        }else{
            //若没有则进行insert操作
            orderSettingDao.addOrderSetting(orderSetting);
        }
    }
}